﻿using System.Collections.Generic;
using VirtualRink.Library.Simulation;

namespace VirtualRink.Library
{
	public class LeagueDaySimulator
	{
		IRepository repository;

		public LeagueDaySimulator()
			: this(new LinqRepository())
		{
		}

		public LeagueDaySimulator(IRepository repository)
		{
			this.repository = repository;
		}

		public void AdvanceAndSimulateDay(League league)
		{
			Season currentSeason = league.GetCurrentSeason();

			league.CurrentDate = league.CurrentDate.AddDays(1);

			IEnumerable<Game> games = this.repository.GetGamesByDate(currentSeason, league.CurrentDate);

			foreach (Game game in games)
			{
				ISimulator simulator = new SimpleGameSimulator();
				simulator.Simulate(game);

				this.repository.Context.ShotCounts.InsertAllOnSubmit(game.ShotCounts);
				this.repository.Context.Goals.InsertAllOnSubmit(game.Goals);
				
			}

			this.repository.SubmitChanges();

			Statistics stats = new Statistics();
			stats.UpdateStatistics(currentSeason.Id);

			this.repository.SubmitChanges();

		}
	}
}
